package me.green.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 日期/时间工具类
 *
 * @author green
 */
public class DateUtil {

    /**
     * 获取某一周的第一天的具体日期 参数weekNum为整数，本周为0，上周为-1，依次递减；下周为1，依次递加
     *
     * @param weekNum int
     * @return
     */
    public static Date getWeekBegin(int weekNum) {
        GregorianCalendar gc = new GregorianCalendar();
        long temp = gc.getTimeInMillis();
        int day_of_week = gc.get(Calendar.DAY_OF_WEEK);
        // 回到周日
        temp = temp - (day_of_week - 1) * 24 * 60 * 60 * 1000;
        temp = temp + weekNum * 7 * 24 * 60 * 60 * 1000;
        java.sql.Date weekBefore = new java.sql.Date(temp);
        return weekBefore;
    }

    /**
     * 获取某一周的最后一天
     *
     * @param beginDate 该周的开始日期
     * @return
     */
    public static Date getWeekEnd(Date beginDate) {
        long temp = beginDate.getTime();
        temp = temp + 7 * 24 * 60 * 60 * 1000;
        java.sql.Date weekEnd = new java.sql.Date(temp);
        return weekEnd;
    }

    /**
     * 格式化日期
     *
     * @param date
     * @return
     */
    public static String formatDate(Date date) {
        String formatString = "yyyy/MM/dd";
        SimpleDateFormat format = new SimpleDateFormat(formatString);
        return format.format(date);
    }

    /**
     * 格式化日期对象为一个可读性更高的字符串
     *
     * @param date
     * @return
     */
    public static String formatDate2ReadString(Date date, boolean fullYearFormat) {
        String formatString = "MM/dd HH:mm E";
        if (fullYearFormat) {
            formatString = "yyyy/MM/dd HH:mm E";
        }
        SimpleDateFormat format = new SimpleDateFormat(formatString);
        return format.format(date);
    }

    /**
     * 得到当前时间的字符串形式，精确到秒
     *
     * @return
     */
    public static String currentDateTime() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(new Date());
    }

}
